package com.alm.token.ui.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.BuyHMioAddData;
import com.alm.token.ui.data.bean.LH2OrderDetailsData;
import com.alm.token.ui.data.bean.QuantitativeBaseChangeData;
import com.alm.token.ui.data.bean.UserListLHData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.QuantitativeOrder2View;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.LogUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import baibeiwallet.Constant;
import baibeiwallet.utils.DialogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Quantitative20OrderCloseActivity extends BaseActivity implements QuantitativeOrder2View<QuantitativeBaseChangeData>, TextWatcher {


    @BindView(R.id.rightItem)
    TextView rightItem;
    @BindView(R.id.tv_bizhongshow)
    TextView tvBizhongshow;
    @BindView(R.id.tv_keyong)
    TextView tvKeyong;
    @BindView(R.id.rly_bizhongselect)
    RelativeLayout rlyBizhongselect;
    @BindView(R.id.edt_inputnumber)
    EditText edtInputnumber;
    @BindView(R.id.tv_bizhongshow2)
    TextView tvBizhongshow2;
    @BindView(R.id.tv_bilv)
    TextView tvBilv;
    @BindView(R.id.ly_ethbody)
    LinearLayout lyEthbody;
    @BindView(R.id.tv_g12show)
    TextView tvG12show;
    @BindView(R.id.tv_g12keyongshow)
    TextView tvG12keyongshow;
    @BindView(R.id.tv_g12keyong)
    TextView tvG12keyong;
    @BindView(R.id.edt_inputg12number)
    EditText edtInputg12number;
    @BindView(R.id.tv_g12show1)
    TextView tvG12show1;
    @BindView(R.id.tv_G12bilv)
    TextView tvG12bilv;
    @BindView(R.id.ly_g12body)
    LinearLayout lyG12body;
    @BindView(R.id.ly_mcoinbody)
    LinearLayout lyMcoinbody;
    @BindView(R.id.tv_appendbilvmath)
    TextView tvAppendbilvmath;
    @BindView(R.id.rly_zhuijiabody)
    RelativeLayout rlyZhuijiabody;
    @BindView(R.id.tv_totalbilvmath)
    TextView tvTotalbilvmath;
    @BindView(R.id.ly_xinjibody)
    LinearLayout lyXxinjiBody;
    @BindView(R.id.tv_zhouqi)
    TextView tvZhouqi;
    @BindView(R.id.tv_yuehua)
    TextView tvYuehua;
    @BindView(R.id.ly_canselect)
    LinearLayout lyCanselect;
    @BindView(R.id.tv_leji)
    TextView tvLeji;
    @BindView(R.id.close_btn)
    TextView closeBtn;
    @BindView(R.id.close_btn1)
    TextView closeBtn1;
    @BindView(R.id.close_body)
    RelativeLayout closeBody;
    @BindView(R.id.switch_isrelogin)
    Switch switch_isrelogin;
    @BindView(R.id.tv_switch_title)
    TextView tvSwitchTitle;
    @BindView(R.id.tv_hsamenum)
    TextView tv_hsamenum;

    //    @BindView(R.id.close_btn1)
//    TextView tvclose_btn1;
    private UserListLHData.DataBean dataBean;
    public static int closeOrderSucc = 3;
    private double guanbibilv = 0d;

    private List<String> zhouqi = new ArrayList<>();
    private List<String> yuehua = new ArrayList<>();
    private List<String> dataBeanBizhong = new ArrayList<>();
    private List<Double> dataBeanCanClosemoney = new ArrayList<>();
    private List<String> bizhong = new ArrayList<>();
    private List<String> bizhongid = new ArrayList<>();
    private List<String> keyong = new ArrayList<>();
    private List<String> jine = new ArrayList<>();
    private List<String> xingji = new ArrayList<>();
    private List<String> bilv = new ArrayList<>();
    private List<Boolean> iscurrent = new ArrayList<>();
    private int bizhongType = 0;
    private double totalMoney = 0.00d, number = 0.00d, g12number = 0;
    ;
    private String biId = "1", bibilv = "0.00";
    public static int AddAppendSucc = 1;
    private List<QuantitativeBaseChangeData.DataBean.CoinBean> coin;
    private QuantitativeBaseChangeData.DataBean.MCoinBean mCoin;
    private double g12canuse = 0.0000d, g12price = 0.00d;
    private String g12name = "G12";


    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_quantitative_order_close20;
    }

    @Override
    protected void initVariables() {
//        dataBean = (UserListLHData.DataBean) getIntent().getSerializableExtra("databean");
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this)
                .setTitle(getString(R.string.title_hint_item29))
                .setRightIcon(R.drawable.lh_order, this);
        switch_isrelogin.setChecked(false);
        switch_isrelogin.setEnabled(false);
        switch_isrelogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mathNumber(g12number, number);
            }
        });
    }


    @Override
    protected void loadData() {
        preenter.getLH2OrderPartOrderShowData();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.close_btn, R.id.rightItem, R.id.rly_bizhongselect, R.id.close_btn1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                if (number == 0 && g12number == 0) {
                    showToast(getString(R.string.alphago_hint_warn5));
                    break;
                }
//                if (dataBean != null) {

                if (xin < 1 && jine != null && jine.size() != 0) {
                    showToast(getString(R.string.alphago_hint_warn1, "" + jine.get(0)));
                    break;
                }

                UserWalletData.DataBean.SysMsgBean sysMsgBean = new UserWalletData.DataBean.SysMsgBean();
                sysMsgBean.setTitle(getString(R.string.alphago_hint_warn9));
                sysMsgBean.setContent(getString(R.string.alphago_hint_warn10));
                DialogUtils.showInfor1(this, sysMsgBean, (pwd) -> {

                    DialogUtils.enterPayPsw(this, (pwd1) -> {

                        preenter.getLH2OrderCloseOrderPartData(getIntent().getStringExtra("Ordersn"),
                                bizhong.get(bizhongType).equalsIgnoreCase("MIO")?biId: iscurrent.get(bizhongType) ? biId : "",bizhong.get(bizhongType).equalsIgnoreCase("MIO")? String.valueOf(number):iscurrent.get(bizhongType) ? String.valueOf(number) : ""
                                ,bizhong.get(bizhongType).equalsIgnoreCase("MIO")? "": !iscurrent.get(bizhongType) ? biId : "", bizhong.get(bizhongType).equalsIgnoreCase("MIO")? "": String.valueOf(number)  , pwd1);

                    });

                });
//                }

                break;
            case R.id.rightItem:
                startActivity(LH2OrderHistoryActivity.class);
                break;
            case R.id.rly_bizhongselect:
//                showToast("选择币种");
//                if (bizhong != null && bizhong.size() != 1) {
//                    showPopupMenu(rlyBizhongSelect);
//                }
                if (dataBeanBizhong != null)
//                    showPopupMenu(rlyBizhongSelect);
                    showPopwindow();
                break;
            case R.id.close_btn1:
//                if (dataBean != null) {
//
//                    UserWalletData.DataBean.SysMsgBean sysMsgBean = new UserWalletData.DataBean.SysMsgBean();
//                    sysMsgBean.setTitle(getString(R.string.alphago_hint_warn9));
//                    sysMsgBean.setContent(getString(R.string.alphago_hint_warn10));
//                    DialogUtils.showInfor1(this, sysMsgBean, (pwd) -> {
//
//                        DialogUtils.enterPayPsw(this, (pwd1) -> {
//                            preenter.getCloseOrderData(dataBean.getOrderid() + "", pwd1);
//                        });
//
//
//                    });
////                    preenter.getCloseOrderData(dataBean.getOrderid() + "");
//                }
                break;
        }
    }

    private int ll1 = -1;
    private int xin = 0;
    private double closemoney = 0.00d;

    private void mathNumber(double g12number, double number) {
        closemoney = number * Double.parseDouble(bibilv)* (switch_isrelogin.isChecked() ? 2 : 1) + g12number * g12price;
        tvAppendbilvmath.setText("≈$" + CommonUtil.saveToPoint2WithNo(closemoney));

        if (closemoney == 0) {
            tvTotalbilvmath.setText("≈$" + CommonUtil.saveToPoint2WithNo(totalMoney));
        } else {
            tvTotalbilvmath.setText("≈$" + CommonUtil.saveToPoint2WithNo(totalMoney - closemoney));
        }

        BigDecimal b1 = new BigDecimal(totalMoney - closemoney);
        double money1 = b1.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();

        LogUtil.e("money1=" + money1);
        if (jine != null && jine.size() != 0) {
            for (int i = 0; i < jine.size(); i++) {
                //"jine":["500.0","2000.0","5000.0","10000.0"]
//                if (number < Double.parseDouble(jine.get(i) != null ? jine.get(i) : "0")) {
                if (money1 < Double.parseDouble(jine.get(i) != null ? jine.get(i) : "0")) {
                    ll1 = i;//2
                    LogUtil.e("ll1_1=" + ll1);
                    break;
                } else {
                    ll1 = jine.size();
                    LogUtil.e("ll1_2=" + ll1);
                }
            }
        }//0无效 1 2 3 4 有效
        xin = ll1;
        if (ll1 == 0) {
            ll1++;
        }
        LogUtil.e("ll1_3=" + ll1);//2
        if (ll1 <= lyXxinjiBody.getChildCount()) {
            for (int i = 0; i < lyXxinjiBody.getChildCount(); i++) {//4number  i=0-3
                if (i < ll1) { //i=0 1 2 3  ll1=1 2 3 4
                    lyXxinjiBody.getChildAt(i).setVisibility(View.VISIBLE);
                } else {
                    lyXxinjiBody.getChildAt(i).setVisibility(View.GONE);
                }
            }
        }
    }

    private void initView() {
        tvBizhongshow.setText(dataBeanBizhong.get(bizhongType));
        tvBizhongshow2.setText(dataBeanBizhong.get(bizhongType));
        switch_isrelogin.setEnabled(false);
        switch_isrelogin.setChecked(bizhong.get(bizhongType).equalsIgnoreCase("MIO") ? false : iscurrent.get(bizhongType));
        tvSwitchTitle.setText(bizhong.get(bizhongType).equalsIgnoreCase("MIO") ? "H" + bizhong.get(bizhongType) + getString(R.string.ago_hint_switch_title_isnotcurrent) :
                iscurrent.get(bizhongType) ? "H" + bizhong.get(bizhongType) + getString(R.string.staking_hint_staking_inputmonmey)
                        + " " + getString(R.string.ago_hint_switch_title_isnotcurrent) :  getString(R.string.ago_hint_switch_title_isnotcurrent));
        if (dataBeanCanClosemoney != null && dataBeanCanClosemoney.size() != 0) {
            tvKeyong.setText(CommonUtil.saveToPoint4WithNo(dataBeanCanClosemoney.get(bizhongType)));
        }
        if (bilv != null && bilv.size() != 0) {
            for (int j = 0; j < bizhong.size(); j++) {
                if (dataBeanBizhong.get(bizhongType).equalsIgnoreCase(bizhong.get(j))) {
                    tvBilv.setText("≈" + bilv.get(j) + "USD/" + bizhong.get(j));
                    biId = bizhongid.get(j);
                    bibilv = bilv.get(j);
                    break;
                }
            }
        }
        edtInputnumber.setText("");
    }

    @Override
    public void getData(QuantitativeBaseChangeData quantitativeBaseChangeData) {

//            showToast(data.getMsg());

        if (quantitativeBaseChangeData.getStatus() != 0)
            showToast(quantitativeBaseChangeData.getMsg());
        if (quantitativeBaseChangeData.getData() != null) {//设置可输入
            zhouqi.clear();
            yuehua.clear();
            bizhong.clear();
            bizhongid.clear();
            iscurrent.clear();
            keyong.clear();
            jine.clear();
            xingji.clear();
            bilv.clear();
            List<QuantitativeBaseChangeData.DataBean.OrderLevelBean> orderLevel = quantitativeBaseChangeData.getData().getOrderLevel();
            for (QuantitativeBaseChangeData.DataBean.OrderLevelBean orderLevelBean : orderLevel) {
                /**
                 * id : 1
                 * zhouqi : 25
                 * yuehua : 0.07
                 * money : 500.0
                 */
                zhouqi.add(orderLevelBean.getZhouqi());
                yuehua.add(orderLevelBean.getYuehua());
                jine.add(orderLevelBean.getMoney() + "");
                xingji.add(orderLevelBean.getId() + "");
            }

            coin = quantitativeBaseChangeData.getData().getCoin();
            for (QuantitativeBaseChangeData.DataBean.CoinBean coinBean : coin) {
                /**
                 * moneyType : 1
                 * name : ETH
                 * yue : 100027.1259
                 * price : 206.64
                 * url : https://api.coom.pub/static/images/eth.png
                 */
                bizhong.add(coinBean.getName());
                dataBeanBizhong.add(coinBean.getName());
                bizhongid.add(coinBean.getMoneyType() + "");
                keyong.add(coinBean.getYue() + "");
                dataBeanCanClosemoney.add(coinBean.getYue());
                bilv.add(coinBean.getPrice());
                iscurrent.add(coinBean.isCurrent());
            }

            mCoin = quantitativeBaseChangeData.getData().getMCoin();
            if (mCoin != null) {
                bizhong.add(mCoin.getName());
                dataBeanBizhong.add(mCoin.getName());
                bizhongid.add(mCoin.getMoneyType() + "");
                keyong.add(mCoin.getYue() + "");
                dataBeanCanClosemoney.add(mCoin.getYue());
                bilv.add(mCoin.getPrice() + "");
                iscurrent.add(mCoin.isCurrent());
            }
//            LogUtil.e(mCoin.toString());

            if (EmptyUtils.isNotEmpty(mCoin) && mCoin.getYue() != 0) {
                lyMcoinbody.setVisibility(View.VISIBLE);
                g12name = mCoin.getName();
                BigDecimal b = new BigDecimal(mCoin.getYue());
                g12canuse = b.setScale(4, BigDecimal.ROUND_HALF_DOWN).doubleValue();
                g12price = mCoin.getPrice();
                tvG12keyong.setText(CommonUtil.saveToPoint4WithNo(g12canuse));
                tvG12show.setText(getString(R.string.alphago_hint_balance, g12name));
                tvG12show1.setText(g12name);
                tvG12bilv.setText("≈" + CommonUtil.saveToPoint4WithNo(g12price) + "USD/" + g12name);
            } else {
                lyMcoinbody.setVisibility(View.GONE);
            }
            if (bizhong != null && bizhong.size() != 0) {
                bizhongType = 0;
                tvBizhongshow.setText(bizhong.get(bizhongType));
                tvBizhongshow2.setText(bizhong.get(bizhongType));
                switch_isrelogin.setEnabled(false);
                switch_isrelogin.setChecked(bizhong.get(bizhongType).equalsIgnoreCase("MIO") ? false : iscurrent.get(bizhongType));
                tvSwitchTitle.setText(bizhong.get(bizhongType).equalsIgnoreCase("MIO") ? "H" + bizhong.get(bizhongType) + getString(R.string.ago_hint_switch_title_isnotcurrent) :
                        iscurrent.get(bizhongType) ? "H" + bizhong.get(bizhongType) + getString(R.string.staking_hint_staking_inputmonmey)
                                + " " + getString(R.string.ago_hint_switch_title_isnotcurrent) :  getString(R.string.ago_hint_switch_title_isnotcurrent));
                if (keyong != null && keyong.size() != 0) {
                    tvKeyong.setText(CommonUtil.saveToPoint4WithNo(Double.valueOf(keyong.get(bizhongType))));
                }
                if (bilv != null && bilv.size() != 0) {
                    tvBilv.setText("≈" + bilv.get(bizhongType) + "USD/" + bizhong.get(bizhongType));
                }
            }

            if (dataBeanCanClosemoney != null) {
//                for (int i = 0; i < dataBeanBizhong.size(); i++) {
//
//                    for (int j = 0; j < bizhong.size(); j++) {
//                        if (dataBeanBizhong.get(i).equalsIgnoreCase(bizhong.get(j))) {
//                            totalMoney = totalMoney + dataBeanCanClosemoney.get(j) * Double.parseDouble(bilv.get(j));
//                            break;
//                        }
//                    }
//                }
                for (int j = 0; j < dataBeanCanClosemoney.size(); j++) {
                    totalMoney = totalMoney + dataBeanCanClosemoney.get(j) * Double.parseDouble(bilv.get(j));
                }

                bizhongType = 0;
                initView();
            }
            mathNumber(g12number, number);
            edtInputnumber.setText("");
            edtInputnumber.addTextChangedListener(this);
            edtInputg12number.setText("");
            edtInputg12number.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.toString().contains(".")) {
                        if (s.length() - 1 - s.toString().indexOf(".") > 4) {
                            s = s.toString().subSequence(0, s.toString().indexOf(".") + 5);
                            edtInputg12number.setText(s);
                            edtInputg12number.setSelection(s.length());
                        }
                    }
                    if (s.toString().trim().substring(0).equals(".")) {
                        s = "0" + s;
                        edtInputg12number.setText(s);
                        edtInputg12number.setSelection(2);
                    }

                    if (s.toString().startsWith("0")
                            && s.toString().trim().length() > 1) {
                        if (!s.toString().substring(1, 2).equals(".")) {
                            edtInputg12number.setText(s.subSequence(0, 1));
                            edtInputg12number.setSelection(1);
                            return;
                        }
                    }

                    g12number = Double.parseDouble(s != null ? (!s.toString().isEmpty() ? s.toString() : "0") : "0");
//                        if (g12number == g12canuse) {
//                            lyEthBody.setVisibility(View.VISIBLE);
//                        } else
                    if (g12number > g12canuse) {
                        showToast(getString(R.string.conversioncurrency_hint1_warn6));
                        g12number = mCoin.getYue();
                        edtInputg12number.setText(CommonUtil.saveToPoint4WithNo(g12number) + "");
                    }
//                        else {
//                            edtInputnumber.setText("");
//                            ethnumber = 0d;
//                            lyEthBody.setVisibility(View.GONE);
//                        }
                    mathNumber(g12number, number);
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });


//            if (quantitativeBaseChangeData.getStatus() != 0)
//                showToast(data.getMsg());
//            if (data.getData() != null) {//设置可输入
//                ShowUserXingjiData.DataBean dataBean = data.getData();
////                zhouqi.addAll(dataBean.getZhouqi());
////                yuehua.addAll(dataBean.getYuehua());
//                bizhong.addAll(dataBean.getBizhong());
//                bizhongid.addAll(dataBean.getBizhongid());
////                keyong.addAll(dataBean.getKeyong());
//                jine.addAll(dataBean.getJine());
//                xingji.addAll(dataBean.getXingji());
//                bilv.addAll(dataBean.getBilv());
//
//                if (dataBeanBizhong != null) {
//                    for (int i = 0; i < dataBeanBizhong.size(); i++) {
//
//                        for (int j = 0; j < bizhong.size(); j++) {
//                            if (dataBeanBizhong.get(i).equalsIgnoreCase(bizhong.get(j))) {
//                                totalMoney = totalMoney + this.dataBean.getBizhongMoney().get(i) * Double.parseDouble(bilv.get(j));
//                                break;
//                            }
//                        }
//                    }
//                    for (int j = 0; j < bizhong.size(); j++) {
//                        if (this.dataBean.getBizhong1().equalsIgnoreCase(bizhong.get(j))) {
//                            totalMoney = totalMoney + this.dataBean.getBizhongMoney1() * Double.parseDouble(bilv.get(j));
//                            break;
//                        }
//                    }
//
//                    bizhongType = 0;
//                    initView();
//                }
//                mathNumber(number);
//                edtInputnumber.setText("");
//                edtInputnumber.addTextChangedListener(this);
//
//            }
        }
    }


    @Override
    public void getLH2OrderOrderDetails(LH2OrderDetailsData lh2OrderDetailsData) {
        if (lh2OrderDetailsData.getStatus() == 0) {
//                showToast(data.getMsg());
            setResult(AddAppendSucc);
            finish();
        } else {
            showToast(lh2OrderDetailsData.getMsg());
        }
    }

    @Override
    public void getLH2OrderOrderDetails(BuyHMioAddData buyHMioAddData) {

    }


    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this, PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType", PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();

    }

    @Override
    public void hideProgress() {
        hideWaitDialog();

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.toString().contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > 4) {
                s = s.toString().subSequence(0, s.toString().indexOf(".") + 5);
                edtInputnumber.setText(s);
                edtInputnumber.setSelection(s.length());
            }
        }
        if (s.toString().trim().substring(0).equals(".")) {
            s = "0" + s;
            edtInputnumber.setText(s);
            edtInputnumber.setSelection(2);
        }

        if (s.toString().startsWith("0")
                && s.toString().trim().length() > 1) {
            if (!s.toString().substring(1, 2).equals(".")) {
                edtInputnumber.setText(s.subSequence(0, 1));
                edtInputnumber.setSelection(1);
                return;
            }
        }
        number = Double.parseDouble(s != null ? (!s.toString().isEmpty() ? s.toString() : "0") : "0");
        if (number > Double.parseDouble(tvKeyong.getText().toString().isEmpty() ? "0" : tvKeyong.getText().toString())) {
            showToast(getString(R.string.conversioncurrency_hint1_warn6));
            number = Double.parseDouble(tvKeyong.getText().toString().isEmpty() ? "0" : tvKeyong.getText().toString());
            edtInputnumber.setText(CommonUtil.saveToPoint4WithNo(number) + "");
        }
        tv_hsamenum.setText(number!=0&&!bizhong.get(bizhongType).equalsIgnoreCase("MIO")&&iscurrent.get(bizhongType)?number+"":"");
        mathNumber(g12number, number);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }


    public void showPopupMenu(View view) {
        //参数View 是设置当前菜单显示的相对于View组件位置，具体位置系统会处理
        PopupMenu popupMenu = new PopupMenu(this, view);
        //加载menu布局
        Menu menu = popupMenu.getMenu();
        for (String bi : dataBeanBizhong) {
            if (!bi.equalsIgnoreCase(Constant.G12ContractName)) {
                menu.add(bi);
            }
        }
        popupMenu.getMenuInflater().inflate(R.menu.menu_popmenu, menu);


        //设置menu中的item点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                for (int i = 0; i < bizhong.size(); i++) {
                    if (item.getTitle().toString().equalsIgnoreCase(dataBeanBizhong.get(i))) {
                        if (item.getTitle().toString().equalsIgnoreCase(Constant.G12ContractName)) {
                            showToast(getString(R.string.crowdfund_hint_warn4));
                            break;
                        }
                        bizhongType = i;
                        initView();
                        mathNumber(g12number, number);
                        break;
                    }
                }
                return true;
            }
        });
        popupMenu.show();
    }

    PopupWindow popWindow;

    private void showPopwindow() {
        View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        View popView = View.inflate(this, R.layout.view_pop_selecter, null);
        LinearLayout ly_pop_body = popView.findViewById(R.id.ly_pop_body);

        if (EmptyUtils.isNotEmpty(coin)) {
            for (int i = 0; i < coin.size(); i++) {
                QuantitativeBaseChangeData.DataBean.CoinBean coinBean = coin.get(i);

                View itemView = View.inflate(this, R.layout.item_pop_wallet, null);
                GlideApp.with(this)
                        .load(coinBean.getUrl())
                        .error(R.drawable.icon_arctic)
                        .into((ImageView) itemView.findViewById(R.id.img_bizhongicon));
                ((TextView) itemView.findViewById(R.id.name)).setText(coinBean.getName());
                ((TextView) itemView.findViewById(R.id.desc)).setVisibility(View.INVISIBLE);


                ((TextView) itemView.findViewById(R.id.money1)).setText(CommonUtil.saveToPoint4WithNo(coinBean.getYue()));
                ((TextView) itemView.findViewById(R.id.money2)).setText("1" + coinBean.getName() + "≈" + CommonUtil.saveToPoint2WithNo(Double.parseDouble(coinBean.getPrice())) + "$");

                int finalI = i;
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bizhongType = finalI;
                        initView();
                        edtInputnumber.setText("");
                        number = 0;
                        edtInputg12number.setText("");
                        g12number = 0;
                        mathNumber(g12number, number);
                        popWindow.dismiss();
                    }
                });
                ly_pop_body.addView(itemView);

            }
            ly_pop_body.setBackgroundColor(getResources().getColor(R.color.text_cccccc));
        }

        RelativeLayout outside = popView.findViewById(R.id.pop_outside);

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        if (popWindow == null) {
            popWindow = new PopupWindow(popView, width, height);
            popWindow.setAnimationStyle(R.style.BottomToTopAnim);
//        popWindow.setFocusable(true);
            popWindow.setOutsideTouchable(true);// 设置同意在外点击消失
        }
        outside.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });

        ColorDrawable dw = new ColorDrawable(0x30000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && popWindow != null && popWindow.isShowing()) {
            popWindow.dismiss();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
